package src.leetcode.tree.simple_0110;

import src.tree.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;


public class isBalanced {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
        TreeNode root = TreeNode.createTree(list);

        Solution solution = new Solution();
        System.out.println(solution.isBalanced(root));
    }
}

class Solution {

    /**
     * 递归
     */
    public boolean isBalanced(TreeNode root) {
        return height(root) >= 0;
    }

    public int height(TreeNode root) {
        if (root == null) return 0;
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);
        if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight- rightHeight) > 1){
            return -1;
        }else {
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }
}
